package com.sq.activity.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.sq.activity.domain.Activity;
import com.sq.activity.mapper.ActivityMapper;
import com.sq.common.core.domain.AjaxResult;
import com.sq.common.core.domain.entity.SysUser;
import com.sq.common.utils.UserThreadLocal;
import com.sq.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sq.activity.mapper.ReviewsMapper;
import com.sq.activity.domain.Reviews;
import com.sq.activity.service.IReviewsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;

/**
 * 评价Service业务层处理
 *
 * @author shan
 * @date 2025-02-13
 */
@Service
public class ReviewsServiceImpl extends ServiceImpl<ReviewsMapper, Reviews> implements IReviewsService {
    @Autowired
    private ReviewsMapper reviewsMapper;

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private SysUserMapper sysUserMapper;
    /**
     * 查询评价
     *
     * @param id 评价主键
     * @return 评价
     */
    @Override
    public Reviews selectReviewsById(Long id) {
        return reviewsMapper.selectonId(id);
    }

    /**
     * 查询评价列表
     *
     * @param reviews 评价
     * @return 评价
     */
    @Override
    public List<Reviews> selectReviewsList(Reviews reviews) {
        return reviewsMapper.selectReviewsList(reviews);
    }

    /**
     * 新增评价
     *
     * @param reviews 评价
     * @return 结果
     */
    @Override
    public AjaxResult insertReviews(Reviews reviews) {
        //非空判断
        if (ObjectUtils.isEmpty(reviews)) {
            return AjaxResult.error("参数不能为空");
        }
        Activity oneByTitle = activityMapper.getOneByTitle(reviews.getActivityTitle());
        reviews.setActivityId(oneByTitle.getId());
        if(reviews.getSysUserNickName().equals("匿名")){
            reviews.setSysUserId(6L);
        }else {
            reviews.setSysUserId(UserThreadLocal.getUserId());
        }
        int i = reviewsMapper.insertReviews(reviews);
        return i>0?AjaxResult.success():AjaxResult.error();
    }

    /**
     * 修改评价
     *
     * @param reviews 评价
     * @return 结果
     */
    @Override
    public int updateReviews(Reviews reviews) {
        return reviewsMapper.updateReviews(reviews);
    }

    /**
     * 批量删除评价
     *
     * @param ids 需要删除的评价主键
     * @return 结果
     */
    @Override
    public int deleteReviewsByIds(Long[] ids) {
        return reviewsMapper.deleteReviewsByIds(ids);
    }

    /**
     * 删除评价信息
     *
     * @param id 评价主键
     * @return 结果
     */
    @Override
    public int deleteReviewsById(Long id) {
        return reviewsMapper.deleteReviewsById(id);
    }
}
